<!--
 * @description  : 内管实地核查动态组件
 * @descriptionDetail: 内管实地核查动态组件
 * @copyright    : 浙江烟草
 * @author       : lcm
 * @create       : 2022-09-27 09:58:58
-->

<template>
  <view>
    <u-form :model="formData" :error-type="errorType" ref="uForm">
      <dynamic-comp-item
        v-for="item in compDataSource"
        :key="item.propName"
        :itemSource="item"
        :bizUuid="bizData.licNo"
        :disabled="disabled"
        @update="handleUpdate"
      ></dynamic-comp-item>
    </u-form>
  </view>
</template>
/** 
  字典项：M_OPERATE_TYPE 
  91401	单选框
  91402	下拉框
  91403	文本框
  91404	文本域
*/
<script>
import DynamicCompItem from "./dynamic-comp-item.vue";
export default {
  name: "DynamicComp",
  components: { DynamicCompItem },
  props: {
    bizData: {
      type: Object,
      default: () => ({}),
    },
    dataSource: {
      type: Array,
      default: () => [],
    },
    disabled: {
      // 是否不可修改
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formData: {},
      errorType: ["border-bottom", "message"],
      compDataSource: [],
    };
  },
  watch: {
    dataSource: {
      deep: true,
      immediate: true,
      handler(v) {
        this.reInitComp(v);
      },
    },
  },
  methods: {
    handleUpdate(itemData) {
      for (let cd of this.compDataSource) {
        if (cd.itemUuid === itemData.itemUuid) {
          cd = itemData;
          break;
        }
      }
      // console.log("父更新", itemData, this.compDataSource);
      this.$emit("update", this.compDataSource);
    },
    validHcItemInfo(h) {
      if (h.problemDesc && h.itemName) {
        return h.itemName + ":" == h.problemDesc.replace(/\s/g, "");
      }
    },
    // 初始化组件
    reInitComp(comps) {
     console.log("初始化组件", comps);
      this.compDataSource = comps.map((c) => {
        let comp = {
          component: this.parseCompName(c.operateType),
          propName: c.itemUuid,
          defaultValue: c.defaultValue,
          unusualValue: c.unusualValue,
          readStatus: this.validHcItemInfo(c),
          label: c.itemName,
          value:
            c.operateType === "91404" ? c.problemDesc : this.initCompValue(c),
          isUpload: true,
        };
        // switch添加备注框
        if (c.operateType === "91401") {
          comp.children = [
            {
              component: "u-input",
              propName: c.itemUuid + "-Text",
              // label: "switch文本框",
              parent: c.itemUuid,
              readStatus: this.validHcItemInfo(c),
              childDefaultVal: c.defaultValue,
              value: c.problemDesc,
              placeholder: "请输入备注内容",
              attrs: {
                type: "textarea",
                height: "100",
                border: true,
              },
            },
          ];
        } else if (c.operateType === "91404") {
          comp.attrs = {
            type: "textarea",
            height: "100",
            border: true,
          };
        }
        return comp;
      });
      // console.log("组件", this.compDataSource);
    },
    // 解析对应的组件
    parseCompName(optType) {
      switch (optType) {
        case "91401": // 是否
          return "u-switch";
        case "91402": // 下拉框
          return "";
        case "91403": // 文本框
          return "";
        case "91404": // 文本域
          return "u-input";
        default:
          break;
      }
    },
    // 处理组件值
    initCompValue(c) {
      if (c.operateType === "91401") {
        if (c.value == undefined) {
          return c.defaultValue == "0" ? false : true;
        }
        let v = c.value || c.defaultValue;
        return typeof v === "boolean" ? v : false;
        // }
      } else if (c.operateType === "91404") {
        return c.value || c.defaultValue || "";
      }
    },
  },
};
</script>
<style lang="scss" scoped>
</style>